{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a04b778b-f54d-484e-9403-113fc137af13",
   "metadata": {},
   "source": [
    "Chapter 09\n",
    "# 定义、使用“列表统计量”类\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5bddf94b-4222-4f6a-be63-e235e198084e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建 ListStatistics 类\n",
    "class ListStatistics:\n",
    "    # 构造函数，用于初始化属性\n",
    "    def __init__(self, data):\n",
    "        # ListStatistics包含一个data属性来存储浮点数列表\n",
    "        self.data = data\n",
    "    \n",
    "    # 下面定义了4个方法\n",
    "    # 方法1：计算列表的长度，即元素的数量\n",
    "    def list_length(self):\n",
    "        return len(self.data)\n",
    "    # 方法2：计算列表元素之和\n",
    "    def list_sum(self):\n",
    "        return sum(self.data)\n",
    "    # 方法3：计算列表元素平均值\n",
    "    def list_mean(self):\n",
    "        return sum(self.data)/self.list_length()\n",
    "    # 方法4：计算列表元素方差\n",
    "    def list_variance(self, ddof = 1):\n",
    "        # Delta自由度 ddof 默认为 1；无偏样本方差\n",
    "        sum_squares = sum((x_i - self.list_mean())**2 \n",
    "                           for x_i in self.data)\n",
    "        return sum_squares/(self.list_length() - ddof)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "22196180-eab8-4a6d-8511-59b6675c139a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建一个浮点数列表\n",
    "data = [8.8, 1.8, 7.8, 3.8, 2.8, 5.6, 3.9, 6.9]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2d93cf8c-0075-4019-a4c0-8272ea977d21",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建ListStatistics对象实例\n",
    "float_list = ListStatistics(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "88a2d0b4-081b-4347-868d-e331f276ccb4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表长度： 8\n"
     ]
    }
   ],
   "source": [
    "# 使用float_list对象计算列表长度\n",
    "print(\"列表长度：\", float_list.list_length())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f9aa85b2-3826-4a79-b161-c5a7e870de37",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表和： 41.4\n"
     ]
    }
   ],
   "source": [
    "# 使用float_list对象计算列表和\n",
    "print(\"列表和：\", float_list.list_sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9a43bf43-c6f6-4634-83e8-0272675cda50",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表平均值： 5.175\n"
     ]
    }
   ],
   "source": [
    "# 使用float_list对象计算列表平均值\n",
    "print(\"列表平均值：\", float_list.list_mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8106fcf7-9370-4cad-ac73-0d9b2a5b7fdd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表方差： 6.247857142857144\n",
      "列表方差 (ddof = 0)： 5.466875000000001\n"
     ]
    }
   ],
   "source": [
    "# 使用float_list对象计算列表方差\n",
    "print(\"列表方差：\", float_list.list_variance())\n",
    "print(\"列表方差 (ddof = 0)：\", \n",
    "      float_list.list_variance(0))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
